﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Logic;
using System.IO;
using System.Data;

namespace EasyCheque.Welcome
{
    public partial class ApplyForCertificate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            if (IsPostBack)
            {
                return;
            }

            if (Session["RegIDCARD"] != null)
            {
                System.Diagnostics.Debug.WriteLine("RegIDCARD SESSION: " + Session["RegIDCARD"]);
                TextBox_IDCARD.Text = Session["RegIDCARD"].ToString();
            }

            if (Session["uid"] != null)
            {
                string CmdStr = @"SELECT IDCARD FROM PERSON_INFO WHERE ID=" + Session["uid"].ToString();
                DataSet DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 1)
                {
                    Panel1.Visible = true;
                    Errinfo.Text = "数据库查询失败，用户不存在";
                    return;
                }
                TextBox_IDCARD.Text = DS.Tables[0].Rows[0][0].ToString();
                TextBox_IDCARD.Enabled = false;
            }
            Panel1.Visible = false;
        }

        protected void Button_Register_Click(object sender, EventArgs e)
        {
            //Button_Reg.Enabled = false;

            Panel1.Visible = true;
            Logic.Logic.LinkDataBase(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(@"\App_Data\EasyCheque.accdb"));
            try
            {
                string UID = "";
                string CmdStr = "";
                DataSet DS = new DataSet();
                if (Session["uid"] != null)
                {
                    UID = Session["uid"].ToString();
                }
                else
                {
                    CmdStr = @"SELECT [ID] FROM [PERSON_INFO] WHERE [IDCARD]='" + TextBox_IDCARD.Text + "'";
                    DS = Logic.Logic.QueryData(CmdStr);
                    if (DS.Tables[0].Rows.Count != 1)
                    {
                        Panel1.Visible = true;
                        Errinfo.Text = "数据库查询失败，用户不存在";
                        return;
                    }
                    UID = DS.Tables[0].Rows[0][0].ToString();
                }
                string HexKey = Security.MD5.HexCompute(TextBox_LoginPW.Text, System.Text.Encoding.ASCII);
                CmdStr = @"SELECT PASSWORD FROM PERSON_INFO WHERE ID=" + UID;
                DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 1)
                {
                    Panel1.Visible = true;
                    Errinfo.Text = "数据库查询失败，密码不存在";
                    return;
                }
                if (HexKey != DS.Tables[0].Rows[0][0].ToString())
                {
                    Panel1.Visible = true;
                    Errinfo.Text = "密码错误";
                    return;
                }
                Panel1.Visible = false;
                MemoryStream MSCer = (MemoryStream)Logic.Logic.RegCer(UID);
                string cerfilename = Security.MD5.HexCompute(UID, System.Text.Encoding.ASCII);
                FileStream fs = new FileStream(Server.MapPath("/CerFile/" + cerfilename + ".cer"), FileMode.Create);
                MSCer.WriteTo(fs);
                fs.Close();
                CmdStr = @"SELECT ACCOUNT_ID FROM PERSON_ACCOUNT WHERE PERSON_ID=" + UID.ToString();
                DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count == 0)
                {
                    Panel1.Visible = true;
                    Errinfo.Text = "内部数据库错误，该身份证无账号";
                    return;
                }
                for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
                {
                    CmdStr = @"UPDATE ACCOUNT_INFO SET [MONEY]=[MONEY]+[USEABLE],[USEABLE]=0 WHERE ACCOUNT_ID='" + DS.Tables[0].Rows[i][0].ToString() + "'";
                    if (!Logic.Logic.UpdateData(CmdStr))
                    {
                        Panel1.Visible = true;
                        Errinfo.Text = "内部数据库错误";
                        return;
                    }
                }

                //// 事后处理。
                //TextBox_IDCARD.Text = "";
                //TextBox_LoginPW.Text = "";

                //下载证书
                EC_DataBase.OperateDB.Insert_into_SafeDown(Convert.ToInt32(UID), "CerFile/" + cerfilename + ".cer");
                Session["uid"] = UID;
                Response.Write("<script>window.open('"+"/SafeDown.aspx?filename=CerFile/" + cerfilename + ".cer'"+")</script>");
                Button_Reg.Enabled = false;
                Button_Reg.Text = "申请成功";

                Session["RegIDCARD"] = null;
                Session["registerSucceed"] = null;
            }
            catch (System.Exception ex)
            {
                Panel1.Visible = true;
                Errinfo.Text = ex.Message.ToString();
                return;
            }
        }
    }
}
